<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>图书借阅系统 - 登录</title>
    <style>
        .login-container {
            width: 350px;
            margin: 100px auto;
            padding: 30px;
            box-shadow: 0 0 10px rgba(0,0,0,0.1);
        }
        .form-group {
            margin-bottom: 20px;
        }
        input {
            width: 100%;
            padding: 8px;
            margin-top: 5px;
        }
        .btn-login {
            width: 100%;
            padding: 10px;
            background: #007bff;
            color: white;
            border: none;
        }
    </style>
</head>
<body>
<div class="login-container">
    <h2>系统登录</h2>
    <form id="loginForm">
        <div class="form-group">
            <label>用户名：</label>
            <input type="text" name="username" required>
        </div>
        <div class="form-group">
            <label>密码：</label>
            <input type="password" name="password" required>
        </div>
        <button class="btn-login" type="submit">登录</button>
    </form>
</div>
<script src="/static/myajax.js"></script>
<script>
    document.getElementById('loginForm').addEventListener('submit', function(e) {
        e.preventDefault();

        const formData = {
            username: this.username.value,
            password: this.password.value
        };

        ajaxRequest( {
            method: 'POST',
            url:"/login",
            data: formData
        }).then(data => {
                if(data.status == 0) {
                    sessionStorage.setItem('token', data.token);
                    sessionStorage.setItem('realname', data.realname);
                    sessionStorage.setItem('user_id', data.user_id);
                    window.location.href = '/'; // 登录成功后跳转
                }else{
                    alert(data.status + '：' + data.msg)
                }
        }).catch(error => alert('Error:'+ error.msg));
    });
</script>
</body>
</html>